package com.dzh.healthflutter.flutter;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.dzh.healthflutter.R;

import io.flutter.FlutterInjector;
import io.flutter.embedding.android.FlutterView;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * @Description: 自己实现的测试view
 * 参考文章:http://laomengit.com/guide/mixing/MethodChannel.html
 * @Author: duzhenhua3
 * @CreateDate: 7/27/21 6:01 PM
 */
public class FlutterDzhViewActivity extends AppCompatActivity {

    private RelativeLayout tvFlutterViewRooot;

    private FlutterViewEngine mFlutterViewEngine;

    private FlutterView mFlutterView;

    private MethodChannel flutterChannel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flutterdzhview);


        tvFlutterViewRooot = findViewById(R.id.tvFlutterViewRooot);

        FlutterEngine engine = new FlutterEngine(getBaseContext().getApplicationContext());
        engine.getDartExecutor().executeDartEntrypoint(new DartExecutor.DartEntrypoint(
                FlutterInjector.instance().flutterLoader().findAppBundlePath(),
                "showDzhView"
        ));
        mFlutterViewEngine = new FlutterViewEngine(engine);
        mFlutterViewEngine.attachToActivity(this);
        flutterChannel = new MethodChannel(mFlutterViewEngine.getEngine().getDartExecutor(), "dev.flutter.example/dzhview");
        new MethodChannelBridge(mFlutterViewEngine.getEngine().getDartExecutor().getBinaryMessenger());

        mFlutterView = new FlutterView(this);
        tvFlutterViewRooot.addView(mFlutterView);

        mFlutterViewEngine.attachFlutterView(mFlutterView);

        flutterChannel.invokeMethod("setParams", "我是Android传递过来的！");

    }


    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    @Override
    protected void onDestroy() {
        if (mFlutterViewEngine != null) {
            mFlutterViewEngine.detachFlutterView();
            mFlutterViewEngine.detachActivity();
        }
        super.onDestroy();
    }

    class MethodChannelBridge implements MethodChannel.MethodCallHandler {

        private MethodChannel channel;

        public MethodChannelBridge(BinaryMessenger messenger) {
            channel = new MethodChannel(messenger, "dev.flutter.example/dzhview");
            channel.setMethodCallHandler(this);
        }

        @Override
        public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
            if (TextUtils.equals(call.method,"sendData")) {
                String name = call.argument("name");
                Toast.makeText(FlutterDzhViewActivity.this, "收到了Flutter的点击事件 name:" + name, Toast.LENGTH_LONG).show();
                int age = call.argument("age");
                result.success("success!!!!!!!!!!!!");
            }
        }
    }
}
